/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-09-02     RT-Thread    first version
 */
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"

#include <arpa/inet.h>
#include <netdev.h>
#include <ntp.h>
#include "key_scan.h"

extern void wlan_autoconnect_init(void);
extern void oled_display(void);
extern int get_NCOV_Data(void);
extern void oled_draw_menu();

int led_toggle(void)
{
 //  rt_pin_write(LED_PIN,!rt_pin_read(KEY2_PIN) );
    static rt_bool_t temp = 0;
    if(!rt_pin_read(KEY1_PIN))  //enter to inside  temper
    {
        rt_pin_write(LED_BLUE,PIN_HIGH);
    }

    if(!rt_pin_read(KEY2_PIN))  //enter into outside weather
    {
        rt_pin_write(LED_BLUE,PIN_LOW);
    }

    if(!rt_pin_read(KEY3_PIN))  //enter into outside weather
    {

        temp = !temp;
     //   rt_kprintf("\n led:%d \n",temp);
        rt_pin_write(LED_RED,temp);
       // set_status(3);
    }

}

int main(void)
{
    rt_uint32_t count = 1;

    /* init Wi-Fi auto connect feature */
    wlan_autoconnect_init();
    /* enable auto reconnect on WLAN device */
    rt_wlan_config_autoreconnect(RT_TRUE);

    struct netdev* net = netdev_get_by_name("esp0");
    while(netdev_is_internet_up(net) != 1)
    {
        set_conneted(0);
        rt_thread_mdelay(200);
    }
    set_conneted(1);
   // oled.drawXBMP(102,0,8,8,on_line);


    rt_thread_mdelay(12000);

    time_t  cur_time = ntp_sync_to_rtc(NULL);
    if(cur_time)
    {
        rt_kprintf("Cur time %s", ctime((const time_t*)&cur_time));
     }
     else
     {
         rt_kprintf("NTP sync fail \n");
     }

    key_thread_entry();
    while(count++)
    {
       rt_thread_mdelay(100);
       oled_display();
    }
    return RT_EOK;
}

//#include "stm32h7xx.h"
static int vtor_config(void)
{
    /* Vector Table Relocation in Internal QSPI_FLASH */
    SCB->VTOR = QSPI_BASE;
    return 0;
}
INIT_BOARD_EXPORT(vtor_config);

// You may reference Drivers/drv_gpio.c for pinout
// In u8x8.h #define U8X8_USE_PINS



